package com.mipo.client.javase.company.flytek.zhoushan.jxtauthourity;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class Product {
	//
	public static void main(String[] args) throws Exception{
		queryStatus();
		//querySubject();
		//queryBooksAndPageInfo();
		//queryTaocan();
	}
	
	
	// 查询状态
	public static void queryStatus() throws Exception {
		String url = "http://zhoushan.book.changyan.cn/index.php?c=home&a=getUserCardState&loginName=zlj&appName=rrt_zhoushan";
		excuteGetRequest(url);
	}
	
	// 获取年级信息
	public static void querySubject() throws Exception  {
		//String url = "http://nbbl.book.changyan.cn/index.php?c=bookstore&a=getDirectoryHandler";
		String url = "http://zhoushan.book.changyan.cn/index.php?c=bookstore&a=getDirectoryHandler";
		excutePostRequest(url);
	}
	
	// 获取书本和分页信息
	public static void queryBooksAndPageInfo() throws Exception  {
		String url = "http://zhoushan.book.changyan.cn/index.php?c=bookstore&a=getBooksHandler";
		excutePostRequest2(url);
	}
	
	public static void queryTaocan()throws Exception{
		String url = "http://zhoushan.book.changyan.cn/index.php?c=home&a=getCardInfo&cardId=7CEC0B04-73FF-0FC8-79BC-CDE408EE6B9D,6EFA13A8-7B5B-3A8D-E06A-713AD59F0A30";
		excuteGetRequest(url);
	}
	
	
	public static void excuteGetRequest(String url) throws Exception {
		System.out.println("请求url："+url);
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = httpClient.execute(httpGet); // 4.3版本不设置超时的话，一旦服务器没有响应，等待时间N久(>24小时)。
		HttpEntity entity = response.getEntity(); // 返回报文实体
		StatusLine status = response.getStatusLine();
		System.out.println("返回的状态码为：" + status); // 具体返回码(200)为status.getStatusCode();
		String ret = EntityUtils.toString(entity); // 将返回报文实体转换为字符串
		System.out.println("返回的报文为：" + URLDecoder.decode(ret,"UTF-8"));
		EntityUtils.consume(entity); // 释放是否可以去掉？
		response.close(); // 关闭资源,可以写在finally里
		httpGet.releaseConnection(); // 释放资源
		httpClient.close(); // 关闭资源
	}
	
	
	public static void excutePostRequest(String url) throws Exception {
		System.out.println("请求url："+url);
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		List<NameValuePair> formPair = new ArrayList<NameValuePair>();
		formPair.add(new BasicNameValuePair("dirname", "subjects"));
		formPair.add(new BasicNameValuePair("grade", ""));
		formPair.add(new BasicNameValuePair("subject", ""));
		formPair.add(new BasicNameValuePair("volumn", ""));
		formPair.add(new BasicNameValuePair("publisher", ""));
		UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(formPair, "UTF-8"); // 对参数进行编码
		httpPost.setEntity(urlEntity);
		CloseableHttpResponse response = httpClient.execute(httpPost); // 4.3版本不设置超时的话，一旦服务器没有响应，等待时间N久(>24小时)。
		HttpEntity entity = response.getEntity(); // 返回报文实体
		StatusLine status = response.getStatusLine();
		System.out.println("返回的状态码为：" + status); // 具体返回码(200)为status.getStatusCode(); 
		String ret = EntityUtils.toString(entity); // 将返回报文实体转换为字符串
		System.out.println("返回的报文为：" + ret);
		EntityUtils.consume(entity);  // 释放是否可以去掉？
		response.close(); // 关闭资源,可以写在finally里
		httpPost.releaseConnection(); // 释放资源
		httpClient.close(); // 关闭资源
	}
	
	public static void excutePostRequest2(String url) throws Exception {
		System.out.println("请求url："+url);
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		List<NameValuePair> formPair = new ArrayList<NameValuePair>();
		formPair.add(new BasicNameValuePair("account", "yewen"));
		formPair.add(new BasicNameValuePair("grade", ""));
		formPair.add(new BasicNameValuePair("subject", ""));
		formPair.add(new BasicNameValuePair("volumn", ""));
		formPair.add(new BasicNameValuePair("publisher", ""));
		formPair.add(new BasicNameValuePair("pageindex", "1"));
		formPair.add(new BasicNameValuePair("pagesize", "15"));
		formPair.add(new BasicNameValuePair("province", "浙江省"));
		UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(formPair, "UTF-8"); // 对参数进行编码
		httpPost.setEntity(urlEntity);
		CloseableHttpResponse response = httpClient.execute(httpPost); // 4.3版本不设置超时的话，一旦服务器没有响应，等待时间N久(>24小时)。
		HttpEntity entity = response.getEntity(); // 返回报文实体
		StatusLine status = response.getStatusLine();
		System.out.println("返回的状态码为：" + status); // 具体返回码(200)为status.getStatusCode(); 
		String ret = EntityUtils.toString(entity); // 将返回报文实体转换为字符串
		System.out.println("返回的报文为：" + ret);
		EntityUtils.consume(entity);  // 释放是否可以去掉？
		response.close(); // 关闭资源,可以写在finally里
		httpPost.releaseConnection(); // 释放资源
		httpClient.close(); // 关闭资源
	}
	
	
}
